<route lang="json5" type="page">
{
  layout: 'default',
  style: {
    navigationBarTitleText: '点单服务列表',
    enablePullDownRefresh: false,
  },
}
</route>

<template>
  <view class="common-tab-list-wrap">
    <wd-tabs
      custom-class="common-header-tabs"
      v-model="tab"
      @change="changeTab"
    >
      <block
        v-for="(item, inx) in tabList"
        :key="inx"
      >
        <wd-tab
          :title="item.title"
          :name="item.value"
        ></wd-tab>
      </block>
    </wd-tabs>
    <view class="content">
      <scroll-view
        scroll-y
        class="scroll-view flex-1"
        refresher-enabled
        enable-back-to-top
        :refresher-triggered="refresherTriggered"
        @refresherrefresh="onRefresherrefresh"
        @scrolltolower="getDataList"
      >
        <view class="data-list">
          <template v-if="dataList.length">
            <view
              v-for="item in dataList"
              :key="item.id"
              class="data-item"
            >
              <!-- <view class="data-item-image common-image-corner">
                <image class="img" :src="formatUrlString(item.coverUrl)[0]" mode="aspectFill" />
              </view> -->
              <view class="flex justify-between">
                <view>
                  <view class="data-item-title">{{ item.name }}</view>
                  <view class="data-item-tip">
                    礼堂：{{ item.hallId_dictText }}
                  </view>
                  <view class="data-item-tip">
                    申报人：{{ item?.userId_dictText }}
                  </view>
                  <view class="data-item-tip">活动日期：{{ item?.date }}</view>
                  <view class="data-item-tip info-address">
                    <wd-icon
                      name="location"
                      size="14px"
                    ></wd-icon>
                    {{ item.address }}
                  </view>
                </view>
                <view class="flex items-end">
                  <JudgePermission action="hall:hall_order_declare:approve">
                    <wd-button
                      size="small"
                      @click="
                        uniNavigateTo(
                          `/pages-sub/order-serve-approve/detail/index?id=${item.id}`,
                        )
                      "
                    >
                      {{ item.status === 'start' ? '审批' : '查看' }}
                    </wd-button>
                  </JudgePermission>
                </view>
              </view>
            </view>
          </template>
          <template v-else>
            <wd-status-tip
              image="content"
              tip="暂无内容"
            />
          </template>
        </view>
        <wd-loadmore
          v-if="dataList.length"
          finished-text="没有更多了"
          :state="loadState"
          @reload="getDataList"
        />
      </scroll-view>
    </view>
  </view>
</template>

<script setup>
import { uniNavigateTo, formatUrlString, currentPageNeedRefresh } from '@/utils'
import OrderApi from '@/service/order'
import JudgePermission from '@/components/JudgePermission.vue'

const tab = ref('start')

const tabList = ref([
  {
    title: '待审批',
    value: 'start',
  },
  {
    title: '已审批',
    value: 'pass',
  },
  {
    title: '已驳回',
    value: 'reject',
  },
])

const changeTab = (event) => {
  tab.value = event.name
  refreshList()
}
const loadState = ref('loading')
const pageObj = ref({
  pageNo: 1,
  pageSize: 10,
})
const dataList = ref([])

const getDataList = async () => {
  console.log(22, loadState.value)
  if (loadState.value === 'finished') return
  try {
    console.log('tab.value', tab.value)
    const params = {
      ...pageObj.value,
      status: tab.value,
      order: 'desc',
      column: 'createTime',
    }
    console.log('params', params)
    const { code, result } = await OrderApi.getOrderDeclareList(params)
    if (code === 200 && result) {
      const { records, pages } = result
      if (records?.length) {
        dataList.value = [...dataList.value, ...records]
      }
      if (pageObj.value.pageNo < pages) {
        pageObj.value.pageNo++
      } else {
        loadState.value = 'finished'
      }
    } else {
      loadState.value = 'error'
    }
  } catch (e) {
    loadState.value = 'error'
    // console.log('e', e)
  }
}

const reset = () => {
  pageObj.value.pageNo = 1
  dataList.value = []
  loadState.value = 'loading'
}

const refreshList = () => {
  reset()
  getDataList()
}

const refresherTriggered = ref(false)
const onRefresherrefresh = async () => {
  refresherTriggered.value = true
  // 重置分页数据，刷新接口
  reset()
  await getDataList()
  refresherTriggered.value = false
}

const updateOrderDeclare = () => {
  refreshList()
}

onLoad(() => {
  refreshList()
  uni.$on('updateOrderDeclare', updateOrderDeclare)
})

onShow(() => {
  if (currentPageNeedRefresh()) {
    refreshList()
  }
})

onUnload(() => {
  uni.$off('updateOrderDeclare', updateOrderDeclare)
})
</script>

<style lang="scss" scoped>
.order-serve {
  position: relative;
}

.scroll-view {
  height: calc(100vh - 46px);
}

.data-list {
  padding: $uni-spacing-col-lg $uni-spacing-row-lg;

  .data-item {
    padding-bottom: $uni-spacing-col-lg;
    margin-bottom: $uni-spacing-col-lg;
    border-bottom: 1px solid $uni-border-color;

    &-image {
      margin-bottom: $uni-spacing-col-base;

      .img {
        height: 200px;
      }
    }

    &-title {
      margin-bottom: $uni-spacing-col-sm;
      font-weight: 600;
    }

    &-tip {
      color: $uni-text-color-grey;
    }
  }
}
</style>
